﻿-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[CountOrderTotal] 
	-- Add the parameters for the stored procedure here
	@ID bigint
AS
BEGIN
	UPDATE TestOrderLine
	SET LineTotal = Amount * p.UnitPrice, UnitPrice = p.UnitPrice
	FROM TestProduct p 
	WHERE p.ID = TestOrderLine.ProductID and OrderID = @ID;
	
	UPDATE TestOrder
	SET TotalAmount = sl.LineSum
	FROM (select OrderID, SUM(LineTotal) as LineSum 
			from TestOrderLine
			group by OrderID) sl
	where ID = @ID and sl.OrderID = @ID
END